$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#List'), 'fiction/getComment');
            this.dataGrid.init().parseToolbar().activeButton(['bt_list_recover','bt_list_del','bt_list_pass','bt_list_unPass','bt_list_hide']);
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;

            //通过
            $('#bt_list_pass').click(function () {
                if (xhExtra.disableButton($(this))) return false;
                $this.funObj().changeStatus(5);
            });

            //不通过
            $("#bt_list_unPass").click(function () {
                if (xhExtra.disableButton($(this))) return false;
                $this.funObj().changeStatus(4);
            });
            //屏蔽
            $('#bt_list_hide').click(function () {
                if (xhExtra.disableButton($(this))) return false;
                $this.funObj().changeStatus(1);
            });

            //恢复
            $('#bt_list_recover').click(function () {
                if (xhExtra.disableButton($(this))) return false;
                $this.funObj().changeStatus(2);
            });

            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.disableButton($(this))) return false;
                $this.funObj().changeStatus(3);
            });

            //搜索
            $('#bt_list_search').click(function () {
                $this.funObj().searchAction();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                lockArticle:function (type,msg) {
                    xhExtra.askChangeStatus('fiction/lockComment/' + type, $this.dataGrid.getSelectValues('id'), $this.dataGrid, msg);
                },
                bindDgEvent:function () {
                    $this.dataGrid.rowStyler = function(rowIndex, rowData){
                        if(rowData.status === '屏蔽'){
                            return 'background-color: yellow';
                        }
                    };
                    $this.dataGrid.loadFilter = function (data) {
                        if(power.editPower) {
                            for (var i = 0; i < data.rows.length; i++){
                                data.rows[i].operate = '<a class="comment-btn comment-pass" onclick="obj.funObj().operateStatus(\'' + data.rows[i].id + '\', 5);">通过</a>&nbsp;&nbsp;' +
                                    ' <a class="comment-btn comment-ignore" onclick="obj.funObj().operateStatus(\'' + data.rows[i].id + '\', 4);">不通过</a>&nbsp;&nbsp;' +
                                    ' <a class="comment-btn comment-forbid" onclick="obj.funObj().operateStatus(\'' + data.rows[i].id + '\', 1);">屏蔽</a>';
                            }
                        }
                        return data;
                    };
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        var str =
                            '<div>' +
                            '   <div id="leftToolBar"  style="display: inline-block;float: left;width: 500px;height: 40px;">' +
                            '     <a class="btn btn-default pull-left" style="color: #00ee00;margin-left: 15px;"><input name="status" type="checkbox" value="5" />通过</a>' +
                            '     <a class="btn btn-default pull-left" style="color: #bf4644;"><input name="status" type="checkbox" value="4" />不通过</a>'+
                            '     <a class="btn btn-default pull-left" style="color: #ffa500;"><input name="status" type="checkbox" value="1" />屏蔽</a>'+
                            '   </div>' +
                            '   <div  id="rightToolBar" style="display: inline-block;float: right;width: 300px;">' +
                            '     <a id="confirm" class="btn btn-default pull-left" style="margin-left: 55px;color: #0099CC">确认</a>' +
                            '     <a id="confirmNext" class="btn btn-default pull-left modal-btn-checked" style="margin-left: 15px;">确认并跳至下一篇</a>' +
                            '   </div>' +
                            '</div>';
                        window.top.xhTop.showModal({
                            title: rowData['articleTitle'],
                            content: '<div style="height: 500px;">' + rowData['content'] + '</div>',
                            width:900,
                            footerHtml: str,
                            backClose:false
                        },function () {  //加载完成后操作
                            $('.modal-title',window.top.document).attr('style','text-align:center;');
                            $('#leftToolBar a', window.top.document).click(function () {  //左侧工具栏点击事件
                                $('#leftToolBar a', window.top.document).each(function () {
                                    $(this).find('input').removeAttr("checked");
                                    var attr = $(this).attr('style');
                                    if($(this).hasClass('modal-btn-checked')){
                                        $(this).removeClass('modal-btn-checked');
                                    }
                                });
                                $(this).find('input').attr('checked','checked').prop("checked",'checked');
                                $(this).addClass('modal-btn-checked');
                            });
                            if (!power.editPower) {
                                $('#rightToolBar').removeAttr('style').hide();
                            }
                            $('#leftToolBar a', window.top.document).each(function () {
                                if (rowData['status'] == $(this).text()){
                                    $(this).click();
                                }
                            });
                            if(rowData['status'] == '审核中'){
                                $('#confirm', window.top.document).unbind('click').on('click',function () {
                                    $this.funObj().confirm(rowData['id']);
                                });
                            }else {
                                $('#confirm', window.top.document).attr('disabled','disabled').attr('style','color:gray');
                            }
                            var nextStatus = '';
                            if($('[datagrid-row-index='+(rowIndex+1)+']').length > 0) {
                                nextStatus = $('[datagrid-row-index='+(rowIndex+1)+']').find('[field="status"] div').text();
                            }
                            if (nextStatus) {
                                $('#confirmNext', window.top.document).unbind('click').on('click',function () {
                                    $this.funObj().confirmNext(rowIndex, $('#confirm', window.top.document).attr('disabled'));
                                });
                            } else {
                                $('#confirmNext', window.top.document).attr('disabled','disabled').attr('style','color:gray').removeClass('modal-btn-checked');
                            }
                        })
                    };
                    $this.dataGrid.onLoadSuccess = function (data) {
                        $.each(data.rows, function (index, row) {
                            $this.funObj().renderStar(row.id);
                        })
                    };
                },
                confirmNext:function (rowIndex,disabled) {
                    if(!disabled && $('#leftToolBar', window.top.document).find('[name=status][checked=checked]').length == 1){ //可以提交
                        $this.funObj().confirm($('[datagrid-row-index='+rowIndex+']').find('[field="id"] div').text());
                    }
                    window.top.xhTop.closeModal();
                    $('[datagrid-row-index='+(rowIndex+1)+']').dblclick();
                },
                confirm:function (id) {
                    var status = $('#leftToolBar', window.top.document).find('[name=status][checked=checked]').val();
                    var ids = [id];
                    xhExtra.ajaxRequest('fiction/dealComment',{action:status, ids:ids},{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    })
                },
                operateStatus:function (id,type) {
                    var index = $this.dataGrid.command('getRowIndex', id);
                    $this.dataGrid.command('updateRow', {
                        index: index,
                        row: {
                            status: '<i class="fa fa-spinner fa-spin"></i>'
                        }
                    });
                    xhExtra.ajaxRequest('fiction/setSingleCommentStatus', {id: id, action: type}, {check: false}, function (data) {
                        if (data.error) {
                            // $this.dataGrid.command('updateRow', {
                            //     index: index,
                            //     row: {
                            //         status: data.error
                            //     }
                            // });
                            window.top.xhTop.showError(data.error);
                            return false;
                        }
                        $this.dataGrid.command('updateRow', {
                            index: index,
                            row: data
                        });
                    });
                },
                changeStatus:function (type) {
                    var ids = $this.dataGrid.getSelectValues('id');
                    xhExtra.ajaxRequest('fiction/dealComment',{action:type, ids:ids},{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    })
                },
                searchAction:function () {
                    $this.dataGrid.reload({
                        key:xhExtra.getValue($('#list_vague')),
                        startTime:xhExtra.getValue($('#list_start')),
                        endTime:xhExtra.getValue($('#list_end')),
                        status:xhExtra.getValue($('#list_status'))
                    })
                },
                renderStar:function (id) {
                    layui.use(['rate'], function() {
                        var target = $('.comment-star[data-id=' + id + ']');
                        layui.rate.render({
                            elem: target,
                            value: target.attr('data-star'),
                            readonly: !power.editPower,
                            choose: function (star) {
                                xhExtra.ajaxRequest('fiction/changeStar', {id: id, star: star}, {check: true}, function (data) {})
                            }
                        });
                    });
                }
            }
        }
    }
};